<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理后台</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/res/css/base.css">
    <link rel="stylesheet" href="/static/res/css/index.css">

</head>
<body>
<script src="/static/layui/layui.js"></script>
<!-- #include file = "../common/header.html" -->
<div class="layui-main">
    <label id="test"></label>
</div>
<audio autoplay="autoplay" id="music" hidden preload="none">
    <source type="audio/ogg">
</audio>
<script src="/static/res/js/web-util.js" charset="utf-8"></script>
<script src="/static/res/js/md5.js" charset="utf-8"></script>
<script>
    var jQuery;
    var $;
    var conn;

    function appendLog(msg) {
        var label=document.getElementById("test");
        var result = label.innerText;
        result = result + msg + "\n";
        label.innerText = result;
    }

    function formatTime () {
        var date = new Date();
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours()
        const minute = date.getMinutes()
        const second = date.getSeconds()

        return [year, month, day].map(formatNumber).join('-') + " " + [hour, minute, second].map(formatNumber).join(':');
    }

    function formatNumber (n) {
        n = n.toString();
        return n[1] ? n : '0' + n
    }

    if (window["WebSocket"]) {
        console.log("111111");
        conn = new WebSocket("ws://www.thumbapi.com:8888");
        conn.onclose = function (evt) {
            appendLog('Connection Closed.')
        };

        conn.onmessage = function (evt) {

            var msg = jQuery.parseJSON(evt.data);


            if (msg.succ) {


                switch (msg.data.messageType) {
                    case 0:
                        var str = msg.data.shopName + "  注册成功  shopId:" + msg.data.shopId

                        appendLog(str);
                        break;
                    case 1:

                        appendLog("注销成功");
                        break;
                    case 2:
                        var tex = msg.data.table + msg.data.ringContent

                        appendLog(formatTime() + "   " + tex);

                        var audio = document.getElementById('music');
                        audio.src = msg.data.url
                        var index = 0;
                        audio.addEventListener('ended', function () {
                            // Wait 500 milliseconds before next loop
                            setTimeout(function () {
                                if (index < 2) {
                                    audio.src = msg.data.url
                                    audio.play();
                                    index++
                                }
                            }, 500);
                        }, false);
                        audio.play();

//                        $.get("http://10.12.25.66:8889/v1/push/speechConversion", {
//                            tex: tex,
//                            cuid: "1234567890qwert",
//                        }, function (result) {
//                        });
                        break
                }
            }
        }
    } else {
        console.log("222222");
        appendLog('WebSockets Not Support.')
    }

    layui.use('layer', function (data) {
        var layer = layui.layer;
        jQuery = layui.jquery;
        $ = layui.jquery;

        var keyArray = ['userId'];
        var valueArray = ['2'];
        getData(apiUrl.api_get_shop_list, true, keyArray, valueArray, function (msg) {
            console.log(msg);
            var result = '';
            if (msg["code"] == 0) {
                var dataArray = msg['data'];
                if (dataArray.length > 0) {
                    var item = dataArray[0];
                    result = result + "\n";
                    result = result + "Address: " + item["Address"] + "\n";
                    result = result + "Code: " + item["Code"] + "\n";
                    result = result + "CreateTime: " + item["CreateTime"] + "\n";
                    result = result + "Id: " + item["Id"] + "\n";
                    result = result + "ShopName: " + item["ShopName"] + "\n";
                    result = result + "Tel: " + item["Tel"] + "\n";
                    result = result + "UserId: " + item["UserId"] + "\n";
                    result = result + "\n";

                    result = result + "消息中心连接信息：\n";


                    var message = "{\"Table\":\"\",\"ShopId\":\"" + dataArray[0]["Id"] + "\",\"ShopName\":\"" + dataArray[0]["ShopName"] + "\", \"Content\":\"\", \"MessageType\":0,\"RingType\":0}";
                    conn.send(message);
                } else {
                    result = "没有对应的店铺信息\n";
                }
                var label = document.getElementById("test");
                label.innerText = result;
            } else {
                layer.msg(msg["msg"]);
            }
        });

        // http://10.12.25.66:8080/v1/apis/user/userTokenVeri?userId=2&timeStamp=1234567890&token=fba7a96bbeca2519948e371669bc57dc
        var userInfo = layui.data('userInfo');
        var privateKey = "50149065";
//        console.log(userInfo["data"]);
        var userId = userInfo["data"]["Id"];
        var timeStamp = new Date().getTime();
        var token = hex_md5(userId + privateKey + "c8ebc145aec5eccfffa96a8c7f20bce9" + privateKey + timeStamp);
        keyArray = ['userId', 'timeStamp', 'token'];
        valueArray = [userId, timeStamp, token];

        console.log(apiUrl.api_token_verify);
        getData(apiUrl.api_token_verify, true, keyArray, valueArray, function (msg) {
            console.log(msg);
        });
    });
</script>
</body>
</html>